from PySide6.QtCore import Qt, QThread, Signal
import subprocess
class Worker(QThread):
    finished = Signal(str)
    def __init__(self, bat_file,logger):
        super().__init__()
        self.bat_file=bat_file
        self.logger=logger

    def run(self):
        try:
            # bat_file = 'install.bat'
            # 使用subprocess.Popen启动批处理文件
            self.logger.log_info(f'begin run [{self.bat_file}]')
            process = subprocess.Popen(self.bat_file, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
            # 循环读取输出
            for line in process.stdout:
                self.logger.log_info(line.strip())  # 实时打印输出
            # 等待进程结束
            process.wait()
            result = "done"
            self.finished.emit(result)
        except Exception as ex:
            self.logger.log_error(ex)